<%@ page import="com.devicemantra.coordinate.dataobjects.*,java.util.*,com.google.appengine.api.datastore.KeyFactory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/css/tab.css" />
<script type="text/javascript">
function initXMLHttpClient() 
{
     var xmlhttp;
     try {
         // Mozilla / Safari / IE7
         xmlhttp = new XMLHttpRequest();
     } 
     catch (e) 
     {
          // IE
          var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
                                     'MSXML2.XMLHTTP.4.0',
                                     'MSXML2.XMLHTTP.3.0',
                                     'MSXML2.XMLHTTP',
                                     'Microsoft.XMLHTTP' );
        var success = false;
          for (var i=0;i < XMLHTTP_IDS.length && !success; i++) 
	  {
              try 
	    	{
                   xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
                      success = true;
                } catch (e) {}
          }
          if (!success) 
	  {
              throw new Error('Unable to create XMLHttpRequest.');
          }
      }
     return xmlhttp;
}



function js_getsubdepts(selection)
{
	var lSelectBox = selection;	
	//alert("Selection was " + lSelectBox.options[lSelectBox.selectedIndex].text);
	document.getElementById('show_window1').innerHTML = "";
	document.getElementById('show_window').innerHTML = "";
	document.getElementById('show_window').innerHTML = "<img src=\"images/animation.gif\" alt=\"Processing...\" />";
	var httpGetReq = initXMLHttpClient();
	httpGetReq.onreadystatechange =function() 
	{
		if(httpGetReq.readyState == 4)
   		{
        		if (httpGetReq.status==200)
        		{
          		//alert(httpGetReq.responseText);
          		//document.getElementById('anim_window').innerHTML = "";
          		document.getElementById('show_window').innerHTML = httpGetReq.responseText;
			}
		}
	}
	
	//alert("GET'ing getWindow.jsp?category=subdepartment&department=" + lSelectBox.options[lSelectBox.selectedIndex].text);
	httpGetReq.open("GET","/modules/administration/getWindow.jsp?category=subdepartment&department=" + lSelectBox.options[lSelectBox.selectedIndex].text,true);
	httpGetReq.send(null);
}
</script>
</head>
<body>
<ul id="tabmenu">
	<li><a href="/AdminController?id=country">Country</a></li>

	<li><a href="/AdminController?id=states">States</a></li>
	<li><a href="/AdminController?id=department">Department</a></li>
	<li><a class="active" href="/AdminController?id=subdepartment">Sub Department</a></li>
	<li><a href="/AdminController?id=users">Users</a></li>
</ul>
<div id="content">
<%
	out.println("<form name=\"add_department\" action=\"/AddSubDepartment\" method=\"POST\">"); 
	List<CDepartment> lDeptList = CDepartmentFactory.getAllDepartments();
	System.out.println("List size isS " + lDeptList.size());
	int lSize = lDeptList.size();
	
	/* Check if department is set by AddSubDepartment , if yes, we need to do
		some changes to the outgoing html */
	//System.out.println("Before getAttribute()");
	String lDeptName = (String)request.getAttribute("DEPT");
	System.out.println("Department name is :" + lDeptName);
	//System.out.println("After getAttribute()");
	boolean lIsFwded = false;
	if(null != lDeptName)
	{
		lIsFwded = true;
		System.out.println("This was a forwared request " );
	}
	else
	{
		System.out.println("This was NOT forwared request " );
	}
	
	if( lSize != 0)
	{
		out.println("<select name=\"dept_name\" onchange=\"js_getsubdepts(this)\">");
		out.print("<option>Select a Department</option>");
		for(int counter = 0; counter < lDeptList.size(); counter++)
		{
			if( false == lIsFwded)
			{
				out.println("<option value=\"" + KeyFactory.keyToString(lDeptList.get(counter).getDepartmentId())
						+ "\">" + lDeptList.get(counter).getDepartmentName() +
						"</option>");
			}
			else
			{
				System.out.println("This was  a forwared request " );
				out.print("<option value=\"" + KeyFactory.keyToString(lDeptList.get(counter).getDepartmentId()) + "\"");
				if(	lDeptList.get(counter).getDepartmentName().equals(lDeptName) )
				{
					System.out.println("************" + lDeptList.get(counter).getDepartmentName() + " = " + lDeptName +  "********");
					out.print(" selected ");
				}
				else
				{
					System.out.println("************" + lDeptList.get(counter).getDepartmentName() + " = " + lDeptName +  "********");
					System.out.println("------------------");
				}
				out.print(">" + lDeptList.get(counter).getDepartmentName() + "</option>"); 
			}
		}
	}
	else
	{
		out.println("<select name=\"dept_name\" >");
		out.println("<option selected=\"true\">NO DEPARTMENTS</option>");
	}
	out.println("</select>");
	out.print("<div id=\"anim_window\"></div>"); 
	out.println("<BR><BR><BR>");
	out.println("<div id=\"show_window\"></div>");
	
	/* 	Show all the sub departments if this is a forwarded request 
	maybe we are returning a response to add subdepartment request ? */
	/* show_window1 should always be available to avoid 
		Error: document.getElementById("show_window1") is null error 
		so it is put outside the conditional  */

	out.print("<div id=\"show_window1\">");
	if( (true == lIsFwded) && (lDeptName != null) )
	{
		
		List<SubDepartment> lSubDeptList  = SubDepartmentFactory.getAllSubDepartments(lDeptName);
		Iterator<SubDepartment> lSubDepartmentIter = lSubDeptList.iterator();
		out.print("<TABLE BORDER=1 COLOR=00FF00>");
			
		int counter = 3;
		while( lSubDepartmentIter.hasNext())
		{
			 if( (counter % 3 ) == 0) 
			 {
				 	out.print("<TR>");
				 	
			 }
				 counter++;
				 SubDepartment lSubDept = lSubDepartmentIter.next();
				 out.print("<TD><input type=\"checkbox\" name=\"vehicle\" value=\"Car\" />" + lSubDept.getSubDepartmentName() +  "</TD>");
				 
				 if( (counter % 3 ) == 0)
				 {
				 	out.print("</TR>");
				 }
				 
		}
			out.print("</TABLE><br><br>");
				
	}
	out.print("</div>");		
			
	out.println("<BR><BR><BR> <INPUT TYPE = text name=\"sub_dept_name\">" + 
	"<BR>" +  "<INPUT TYPE = SUBMIT name=\"button1\" VALUE=\"Add\">" + 
	"<INPUT TYPE = SUBMIT name=\"button2\" VALUE=\"Delete\" >" + 
	"</form>");
%>
  
  
</div>

</body>
</html>